using System;
using System.Data;
using System.Data.SqlTypes;
using System.Data.SqlClient;

namespace Model
{
	public partial class Tbl011_Item : DBInteractionBase
	{
		#region Class Member Declarations
			private SqlBoolean		_adStatus, _statusVIP;
			private SqlInt32		_adID, _cityID;
			private SqlString		_caID, _caIDOld;
			private SqlGuid			_userID, _userIDOld;
			private SqlString		_adDeadLine, _adTitle, _adContent, _adCreateDate;
		#endregion


		public Tbl011_Item()
		{
			// Nothing for now.
		}


		public override bool Insert()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl011_Item_Insert]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@AdID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _adID));
				cmdToExecute.Parameters.Add(new SqlParameter("@AdTitle", SqlDbType.NVarChar, 500, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _adTitle));
				int	length = 0;
				if(!_adContent.IsNull)
				{
					length = _adContent.Value.Length;
				}
				cmdToExecute.Parameters.Add(new SqlParameter("@AdContent", SqlDbType.NText, length, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _adContent));
				cmdToExecute.Parameters.Add(new SqlParameter("@AdCreateDate", SqlDbType.VarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _adCreateDate));
				cmdToExecute.Parameters.Add(new SqlParameter("@AdDeadLine", SqlDbType.VarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _adDeadLine));
				cmdToExecute.Parameters.Add(new SqlParameter("@AdStatus", SqlDbType.Bit, 1, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _adStatus));
				cmdToExecute.Parameters.Add(new SqlParameter("@UserID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _userID));
				cmdToExecute.Parameters.Add(new SqlParameter("@CityID", SqlDbType.Int, 4, ParameterDirection.Input, true, 10, 0, "", DataRowVersion.Proposed, _cityID));
				cmdToExecute.Parameters.Add(new SqlParameter("@CaID", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _caID));
				cmdToExecute.Parameters.Add(new SqlParameter("@StatusVIP", SqlDbType.Bit, 1, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _statusVIP));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl011_Item::Insert::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
			}
		}


		public override bool Update()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl011_Item_Update]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@AdID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _adID));
				cmdToExecute.Parameters.Add(new SqlParameter("@AdTitle", SqlDbType.NVarChar, 500, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _adTitle));
				int	length = 0;
				if(!_adContent.IsNull)
				{
					length = _adContent.Value.Length;
				}
				cmdToExecute.Parameters.Add(new SqlParameter("@AdContent", SqlDbType.NText, length, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _adContent));
				cmdToExecute.Parameters.Add(new SqlParameter("@AdCreateDate", SqlDbType.VarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _adCreateDate));
				cmdToExecute.Parameters.Add(new SqlParameter("@AdDeadLine", SqlDbType.VarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _adDeadLine));
				cmdToExecute.Parameters.Add(new SqlParameter("@AdStatus", SqlDbType.Bit, 1, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _adStatus));
				cmdToExecute.Parameters.Add(new SqlParameter("@UserID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _userID));
				cmdToExecute.Parameters.Add(new SqlParameter("@CityID", SqlDbType.Int, 4, ParameterDirection.Input, true, 10, 0, "", DataRowVersion.Proposed, _cityID));
				cmdToExecute.Parameters.Add(new SqlParameter("@CaID", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _caID));
				cmdToExecute.Parameters.Add(new SqlParameter("@StatusVIP", SqlDbType.Bit, 1, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _statusVIP));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl011_Item::Update::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
			}
		}


		public bool UpdateAllWUserIDLogic()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl011_Item_UpdateAllWUserIDLogic]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@UserID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _userID));
				cmdToExecute.Parameters.Add(new SqlParameter("@UserIDOld", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _userIDOld));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl011_Item::UpdateAllWUserIDLogic::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
			}
		}


		public bool UpdateAllWCaIDLogic()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl011_Item_UpdateAllWCaIDLogic]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@CaID", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _caID));
				cmdToExecute.Parameters.Add(new SqlParameter("@CaIDOld", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _caIDOld));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl011_Item::UpdateAllWCaIDLogic::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
			}
		}


		public override bool Delete()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl011_Item_Delete]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@AdID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _adID));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl011_Item::Delete::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
			}
		}


		public override DataTable SelectOne()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl011_Item_SelectOne]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("tbl011_Item");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@AdID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _adID));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				adapter.Fill(toReturn);
				if(toReturn.Rows.Count > 0)
				{
					_adID = (Int32)toReturn.Rows[0]["AdID"];
					_adTitle = toReturn.Rows[0]["AdTitle"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["AdTitle"];
					_adContent = toReturn.Rows[0]["AdContent"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["AdContent"];
					_adCreateDate = toReturn.Rows[0]["AdCreateDate"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["AdCreateDate"];
					_adDeadLine = toReturn.Rows[0]["AdDeadLine"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["AdDeadLine"];
					_adStatus = toReturn.Rows[0]["AdStatus"] == System.DBNull.Value ? SqlBoolean.Null : (bool)toReturn.Rows[0]["AdStatus"];
					_userID = toReturn.Rows[0]["UserID"] == System.DBNull.Value ? SqlGuid.Null : (Guid)toReturn.Rows[0]["UserID"];
					_cityID = toReturn.Rows[0]["CityID"] == System.DBNull.Value ? SqlInt32.Null : (Int32)toReturn.Rows[0]["CityID"];
					_caID = toReturn.Rows[0]["CaID"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["CaID"];
					_statusVIP = toReturn.Rows[0]["StatusVIP"] == System.DBNull.Value ? SqlBoolean.Null : (bool)toReturn.Rows[0]["StatusVIP"];
				}
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl011_Item::SelectOne::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		public override DataTable SelectAll()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl011_Item_SelectAll]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("tbl011_Item");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				adapter.Fill(toReturn);
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl011_Item::SelectAll::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		public DataTable SelectAllWUserIDLogic()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl011_Item_SelectAllWUserIDLogic]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("tbl011_Item");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@UserID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _userID));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				adapter.Fill(toReturn);
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl011_Item::SelectAllWUserIDLogic::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		public DataTable SelectAllWCaIDLogic()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl011_Item_SelectAllWCaIDLogic]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("tbl011_Item");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@CaID", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _caID));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				adapter.Fill(toReturn);
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl011_Item::SelectAllWCaIDLogic::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		#region Class Property Declarations
		public SqlInt32 AdID
		{
			get
			{
				return _adID;
			}
			set
			{
				SqlInt32 adIDTmp = (SqlInt32)value;
				if(adIDTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("AdID", "AdID can't be NULL");
				}
				_adID = value;
			}
		}


		public SqlString AdTitle
		{
			get
			{
				return _adTitle;
			}
			set
			{
				_adTitle = value;
			}
		}


		public SqlString AdContent
		{
			get
			{
				return _adContent;
			}
			set
			{
				_adContent = value;
			}
		}


		public SqlString AdCreateDate
		{
			get
			{
				return _adCreateDate;
			}
			set
			{
				_adCreateDate = value;
			}
		}


		public SqlString AdDeadLine
		{
			get
			{
				return _adDeadLine;
			}
			set
			{
				_adDeadLine = value;
			}
		}


		public SqlBoolean AdStatus
		{
			get
			{
				return _adStatus;
			}
			set
			{
				_adStatus = value;
			}
		}


		public SqlGuid UserID
		{
			get
			{
				return _userID;
			}
			set
			{
				_userID = value;
			}
		}
		public SqlGuid UserIDOld
		{
			get
			{
				return _userIDOld;
			}
			set
			{
				_userIDOld = value;
			}
		}


		public SqlInt32 CityID
		{
			get
			{
				return _cityID;
			}
			set
			{
				_cityID = value;
			}
		}


		public SqlString CaID
		{
			get
			{
				return _caID;
			}
			set
			{
				_caID = value;
			}
		}
		public SqlString CaIDOld
		{
			get
			{
				return _caIDOld;
			}
			set
			{
				_caIDOld = value;
			}
		}


		public SqlBoolean StatusVIP
		{
			get
			{
				return _statusVIP;
			}
			set
			{
				_statusVIP = value;
			}
		}
		#endregion
	}
}
